package com.liehu.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.liehu.business.dao.ReplyMapper;
import com.liehu.business.pojo.Reply;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional //事务
public class ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private HttpServletRequest request;


    /**
     * 查询所有
     */
    public List<Reply> findAll(){
        return replyMapper.selectList(null);
    }
    /**
     * 根据id查询
     */
    public Reply findById(String id){
        return replyMapper.selectById(id);
    }

    /**
     * 插入
     */
    public void save(Reply reply){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }

        String adminId = claims_admin.getId();
        //给这条记录插入id
        reply.setId(idWorker.nextId()+"");
        reply.setEmployeeId(adminId);
        reply.setNickName("猎狐人才科技");
        replyMapper.insert(reply);
    }

    /**
     * 更新
     */
    public void update(Reply reply){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }
        replyMapper.updateById(reply);
    }
    /**
     * 根据id删除
     */
    public void deleteById(String id){

        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(claims_admin == null ){
            throw new RuntimeException("权限不足！");
        }
        replyMapper.deleteById(id);
    }
    /**
     * 分页查询
     */
    public Page<Reply> findByPage(Integer page,Integer size){
        Page<Reply> pageable = new Page<>(page,size);//创建分页对象
        return  replyMapper.selectPage(pageable, null);
    }
    /**
     * 条件分页查询
     */
    public Page<Reply> findByPage(Reply reply,Integer page,Integer size){
        Page<Reply> pageable = new Page<>(page,size);//创建分页对象
        QueryWrapper<Reply> wrapper = myWrapper(reply);//创建条件对象wrapper
        return  replyMapper.selectPage(pageable, wrapper);
    }

    /**
     * 内部处理条件
     * @param reply
     * @return
     */
    private QueryWrapper<Reply> myWrapper(Reply reply){
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        //条件构造1
        if(!reply.getContent().isEmpty()){
            wrapper.like("content","%"+reply.getContent()+"%");
        }
        //条件构造2
        if(reply.getNickName()!=null&&!reply.getNickName().equals("")){
            wrapper.like("nickName","%"+reply.getNickName()+"%");
        }
        //条件构造3
        if(reply.getId()!=null&&!reply.getId().equals("")){
            wrapper.eq("id",reply.getId());
        }
        //条件构造4
        if(reply.getEmployeeId()!=null&&!reply.getEmployeeId().equals("")){
            wrapper.eq("employeeId",reply.getEmployeeId());
        }
        //条件构造5
        if(reply.getLeaveMessageId()!=null&&!reply.getLeaveMessageId().equals("")){
            wrapper.eq("leaveMessageId",reply.getLeaveMessageId());
        }
        return  wrapper;
    }
}
